<?php

class ProgrammeAddAction extends BaseAction 
{
	/**
	 * @isnull false
	 * @filter string_number
	 * @maxLength 255
	 * @label Programme Path
	 */
	public $path;
    
	/**
	 * @maxLength 255
	 * @label Programme Title(chinese)
	 */
	public $title;

	public $effective_date;

    /**
	 * @maxLength 255
	 * @label Programme No.
	 */
	public $cpl_id;

	public $cpl_company_code;

	/**
 	 * @maxLength 255
	 * @label Programme title(english)
	 */
	public $title_en;
	public $cpl_category_id;
	public $ut;

	public function execute(){
       
		Model::$dbo['w']->beginTransaction();
		try{
			if (count($this->_errors) > 0)
			{
				$this->init_page_data();
				return "filter-error";
			}

			//check name unique
			if (is_null($this->title) && is_null($this->title_en))
			{
				$this->init_page_data();
				$this->addError('Programme.Programme Title', array('0037', 'Programme Title'));
				return 'filter-error';
			}

            if(!empty($this->cpl_company_code) && empty($this->cpl_id))
            {
                $this->init_page_data();
				$this->addError('Programme.cpl_id', array('0011', 'Programme No.'));
				return 'filter-error';
            }
            
            if(!empty($this->cpl_company_code) && empty($this->effective_date))
            {
                $this->init_page_data();
				$this->addError('Programme.effective_date', array('0012', 'Effective Date'));
				return 'filter-error';
            }

			//check path repeat
			if (programme::checkPath(trim($this->path)))
			{
				$this->addError('path', array('0100','path:'.$this->path));
				$this->init_page_data();
				return 'logic-error';
            }
            
			//check cpl no and company code
			if ($this->cpl_id && $this->cpl_company_code){
              	if(programme_cpl::checkCpl(trim($this->cpl_id), $this->cpl_company_code))
				{
					$this->addError('cpl_id', array('0104','Programme no & Company code'));
					$this->init_page_data();
					return 'logic-error';
				}
			}
            

			# change_log
			$this->change_log = array();
			$insert_id = $this->add_programme();
			$this->add_programme_language($insert_id);
			$this->add_multiple_language($insert_id);
			$this->add_tvb_category($insert_id);
			if($this->cpl_id && $this->cpl_company_code){
				$this->add_programme_cpl($insert_id);
			}
            if($insert_id)
            {
                // for bug 6583
                $this->initPremium($insert_id);
            }

			# save change_log
			array_walk($this->change_log,'change_log::updateOne');

			Model::$dbo['w']->commit();

			$this->setData('id', $insert_id);
			$this->setData('type', 'programme');
			$this->setData('act', 'add');
			$this->setData('step', 2);
			return 'succ';
		}
		catch(Exception $e)
		{
			error_log("Add programme Fails: " . var_export($this, 1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}    

    private function initPremium($programme_id)
    {
        // init set subscription 
        $programme_subscription = new programme_subscription;
        $programme_subscription->set($this);
        $programme_subscription->programme_id = $programme_id;
        $programme_subscription->insert($programme_subscription);
        
        $programme = new programme();
        $programme_detail = $programme->getDetails($programme_id);
		$programme_name = explode("\n\t",$programme_detail->mix_programme_name);
        $zh_programme_name = isset($programme_name[0]) ? $programme_name[0] : '';
        $en_programme_name = isset($programme_name[1]) ? $programme_name[1] : '';

        $programme_premium = new programme_premium;
        #$programme_premium->set($this);
        $programme_premium->programme_id =  $programme_id;
        $programme_premium->title_en =  $en_programme_name;
        $programme_premium->title_zh =  $zh_programme_name;
        $programme_premium->subscription_flag = 1;
        $programme_premium->insert($programme_premium);

		$this->change_log[] = array(
			'type' => 'programme_premium',
			'keys' => array('programme_id'=>$programme_id),
			'is_active'=>1,
			'is_publish'=> 0,
			'programme_id'=>$programme_id,
		);
		$this->change_log[] = array(
			'type' => 'programme_subscription',
			'keys' => array('programme_id'=>$programme_id),
			'is_active'=>1,
			'is_publish'=> 0,
			'programme_id'=>$programme_id,
		);
    }

	/**
	 * add base programme data
	 */
	private function add_programme()
	{
		$programme = new programme();
		$programme->set($this);
		$programme->folders = CommonUtils::init_folder("programme", $this);
		$programme->created_at = date("Y-m-d H:i:s");
		$programme->created_by = CommonUtils::get_user();
        $programme->modified_at = $programme->created_at;
        $programme->modified_by = $programme->created_by;
		$programme->status = AC::STATUS_NEW;
		$programme->premium_flag = 1; //default set true
		$programme->available_tag_types = CommonUtils::get_allow_tags("programme");
		$programme->highlighted_cast_roles = CommonUtils::get_highlight_role("programme");
        $insert_id = Model::$dbo['w']->save($programme);
		if($insert_id>0)
		{
			$this->_log_programme_id = $insert_id; // for change_log in CommonUtils init_folder function 
			$this->_log_episode_id   = array();    // for change_log in CommonUtils init_folder function
			$this->change_log[] = array(
				'type' => 'programme', 
				'keys' => array('id'=>$insert_id), 
				'is_active'=>1,
				'is_publish'=> 0,
				'programme_id'=>$insert_id
			);
			$this->addInfo("programme:".AC::LOG_OP_CREATE.":".$insert_id, "add programme success", json_encode($programme));
			return $insert_id;
		}
		else
		{
			$this->addError("programme:".AC::LOG_OP_CREATE.":".$insert_id, "add programme failed", json_encode($programme));
			return false;
		}
	}

	/**
	 * add programme language data 
	 */
	private function add_programme_language($programme_id)
	{
		$programme_language = new programme_language();
		$programme_language->set($this);
		$programme_language->language_id = language::getLanguageID(AC::DEFAULT_LANGUAGE);
		$programme_language->programme_id= $programme_id;

		$ret = Model::$dbo['w']->save($programme_language);
		if ($ret >= 0)
		{
			$this->change_log[] = array(
				'type' => 'programme-language',
				'keys' => array('programme_id'=>$programme_id,'language_id'=>$programme_language->language_id),
				'is_active'=>1,
				'is_publish'=> 0,
				'programme_id'=>$programme_id,
			);
			$this->addInfo("programme-language:".AC::LOG_OP_CREATE.":".$programme_id, "add programme language success", json_encode($programme_language));
		}
		else
		{
			$this->addError("programme-language:".AC::LOG_OP_CREATE.":".$programme_id, "add programme language failed", json_encode($programme_language));
		}
	}

	/**
	 * multiple language support temprary
	 */
	private function add_multiple_language($programme_id)
	{
		$programme_language = new programme_language();
		$programme_language->set($this);
		$programme_language->language_id = language::getLanguageID(AC::LANGUAGE_EN);
		$programme_language->programme_id= $programme_id;
		$programme_language->title = is_null($this->title_en) ? "" : trim($this->title_en);

		$ret = Model::$dbo['w']->save($programme_language);
		if ($ret >= 0)
		{
			$this->change_log[] = array(
				'type' => 'programme-language',
				'keys' => array('programme_id'=>$programme_id,'language_id'=>$programme_language->language_id),
				'is_active'=>1,
				'is_publish'=> 0,
				'programme_id'=>$programme_id,
			);
			$this->addInfo("programme-language:".AC::LOG_OP_CREATE.":".$programme_id, "add programme language success", json_encode($programme_language));
		}
		else
		{
			$this->addError("programme-language:".AC::LOG_OP_CREATE.":".$programme_id, "add programme language failed", json_encode($programme_language));
		}
	}

	/**
	 * add cpl data
	 */
	private function add_programme_cpl($programme_id)
	{
		$programme_cpl = new programme_cpl();
		$programme_cpl->set($this);
		$programme_cpl->programme_id= $programme_id;
		$ret = Model::$dbo['w']->save($programme_cpl);
		if ($ret >= 0)
		{
			$effective_date = (strlen($programme_cpl->effective_date)>10) ? ($programme_cpl->effective_date) : ($programme_cpl->effective_date.' 00:00:00');
			$this->change_log[] = array(
				'type' => 'programme_cpl',
				'keys' => array('programme_id'=>$programme_id,'effective_date'=>$effective_date),
				'is_active'=>1,
				'is_publish'=> 0,
				'programme_id'=>$programme_id,
			);
			$this->addInfo("programme_cpl:".AC::LOG_OP_CREATE.":".$programme_id, "add programme cpl success", json_encode($programme_cpl));
		}
		else
		{
			$this->addError("programme_cpl:".AC::LOG_OP_CREATE.":".$programme_id, "add programme cpl failed", json_encode($programme_cpl));
		}

	}

	/**
	 * get company code list
	 */
	private function get_company_code_list()
	{
		$company_code = new company_code();
		$company_code_list = $company_code->get();
		$company_code_arr  = array();
		foreach ($company_code_list as $company_code)
		{
			$company_code_arr[$company_code->code] = $company_code->system_label;
		}
		$this->setData("company_code_list", $company_code_arr);
	}

	/**
	 * add tvb category data
	 */
	private function add_tvb_category($programme_id)
	{
		$programme_tvbcom_category = new programme_tvbcom_category();		

		if (!empty($this->cpl_category_id))
		{
			$programme_tvbcom_category->programme_id = $programme_id;
			$programme_tvbcom_category->category_id  = cpl_category::getTvbcomCategory($this->cpl_category_id);
			$ret = Model::$dbo['w']->save($programme_tvbcom_category);
			if ($ret >= 0)
			{
				$this->change_log[] = array(
					'type' => 'programme-tvbcom_category',
					'keys' => array('programme_id'=>$programme_id,'category_id'=>$programme_tvbcom_category->category_id),
					'is_active'=>1,
					'is_publish'=> 0,
					'programme_id'=>$programme_id,
				);
				$this->addInfo("programme-tvbcom_category:".AC::LOG_OP_CREATE.":".$programme_id, "update programme tvbcom category success", json_encode($programme_tvbcom_category));
			}
			else
			{
				$this->addError("programme-tvbcom_category:".AC::LOG_OP_CREATE.":".$programme_id, "update programme tvbcom category failed", json_encode($programme_tvbcom_category));
			}
		}
	}
	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$this->get_company_code_list();
		$this->setData('cpl_id', $this->cpl_id);
		$this->setData('cpl_company_code', $this->cpl_company_code);
		$this->setData('effective_date', $this->effective_date);
		$this->setData('title', $this->title);
		$this->setData('title_en', $this->title_en);
		$this->setData('cpl_category_id', $this->cpl_category_id);
		$this->setData('path', $this->path);

		//jump page
		$this->setData('type', 'programme');
		$this->setData('act', 'add');
		$this->setData('step', 1);
	}
}
?>
